package com.android.clinic.doctors.app.ui.widget;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.android.clinic.doctors.app.R;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ClipPagerTitleView;

/**
 * Created by cheng on 2018/6/19.
 */
public class MagicIndicatorTools {

    private Context mContext;
    private String[] mArrays;
    private boolean isIndicator;
    private boolean isAdjustMode;
    private boolean isSkimOver;
    private OnClipPagerClickListener mOnClipPagerClickListener;

    private FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();

    public MagicIndicatorTools(Context context) {
        mContext = context;
    }

    public MagicIndicatorTools setArrays(String[] arrays) {
        mArrays = arrays;
        return this;
    }

    /**
     * 是否需要设置指示线(底部的横线)
     * @param isIndicator
     * @return
     */
    public MagicIndicatorTools setIndicator(boolean isIndicator) {
        this.isIndicator = isIndicator;
        return this;
    }

    /**
     * 是否需要设置等分模式
     * @param isAdjustMode
     * @return
     */
    public MagicIndicatorTools setAdjustMode(boolean isAdjustMode) {
        this.isAdjustMode = isAdjustMode;
        return this;
    }

    /**
     * 跨多页切换时，中间页是否显示 "掠过" 效果
     * @param isSkimOver
     * @return
     */
    public MagicIndicatorTools setSkimOver(boolean isSkimOver) {
        this.isSkimOver = isSkimOver;
        return this;
    }

    public MagicIndicatorTools builder(MagicIndicator magicIndicator) {
        CommonNavigator commonNavigator  = new CommonNavigator(mContext);
        commonNavigator.setAdjustMode(isAdjustMode);
        commonNavigator.setSkimOver(isSkimOver);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mArrays.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ClipPagerTitleView clipPagerTitleView = new ClipPagerTitleView(context);
                clipPagerTitleView.setText(mArrays[index]);
                clipPagerTitleView.setTextColor(ContextCompat.getColor(mContext, R.color.text_black3));
                clipPagerTitleView.setClipColor(ContextCompat.getColor(mContext, R.color.clinic_green));
                clipPagerTitleView.setOnClickListener(v -> {
                    mFragmentContainerHelper.handlePageSelected(index);
                    if (null != mOnClipPagerClickListener) {
                        mOnClipPagerClickListener.onClipPager(v, index);
                    }

                });
                return clipPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                if (isIndicator) {
                    LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                    linePagerIndicator.setColors(ContextCompat.getColor(mContext, R.color.clinic_green));
                    linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                    return linePagerIndicator;
                } else {
                    return null;
                }
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        mFragmentContainerHelper.attachMagicIndicator(magicIndicator);
        return this;
    }

    public MagicIndicatorTools setOnClipPagerClickListener(OnClipPagerClickListener mOnClipPagerClickListener) {
        this.mOnClipPagerClickListener = mOnClipPagerClickListener;
        return this;
    }

    public interface OnClipPagerClickListener {
        void onClipPager(View v, int position);
    }

}
